package com.manager.core.module.manager.service;


import com.manager.commons.base.DataGrid;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.core.module.manager.dao.GeneralTopicCategoryDao;
import com.manager.core.module.manager.entity.ComboTreeList;
import com.manager.core.module.manager.entity.article.GeneralTopicCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * PlatformUserLogService
 *
 * @author H
 * @version 1.0
 */
@Service
public class GeneralTopicCategoryService extends GenericService<GeneralTopicCategory> {

    @Autowired
    private GeneralTopicCategoryDao jdbcDao;

    @Override
    public GenericDao<GeneralTopicCategory> getDao() {
        return jdbcDao;
    }

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    public DataGrid queryPage(Map<String, Object> params) {
        try {
            if (null != params) {
                int count = jdbcDao.countMap(params);
                List<GeneralTopicCategory> list = jdbcDao.queryMap(params);
                queryChildren(list,params);
                return new DataGrid(count, list);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new DataGrid(0, new ArrayList<GeneralTopicCategory>());
    }

    private void queryChildren(List<GeneralTopicCategory> list, Map<String, Object> params) {
        for (GeneralTopicCategory gtc : list) {
            params.put("parent_code", gtc.getCode());
            List<GeneralTopicCategory> l = jdbcDao.queryMap(params);
            if (l != null && !l.isEmpty()) {
                gtc.setChildren(l);
                queryChildren(l, params);
            }
        }
    }
    public List<ComboTreeList> queryTreeList() {
        return jdbcDao.queryTree();
    }

}